module app.middleware.CheckIdValidity;

import hunt.framework;
import app.response.ResultMsg;
import app.utils.RespCodeEnun;

/**
* 检查请求中的id是否合法的中间件
*/
class CheckIdValidity : AbstractMiddleware
{
    shared static this()
    {
        MiddlewareInterface.register!(typeof(this));
    }

    Response onProcess(Request req, Response resp)
    {
        import std.regex;

        string id = req.get("id", "");
        // 进行id检查
        auto m = matchFirst(id, regex(`^[1-9]+$`));
        if (m.empty) // id匹配为空
        {
            resp.setStatus(404);
            return new JsonResponse(new ResultMsg(RespCodeEnun.Bad_Request,
                    "错误的请求，请检查id参数"));
            // return new JsonResponse(new ResultMsg(RespCodeEnun.Bad_Request, "id中间件测试"));
        }
        return null;
    }
}
